<template>
  <div>
    <el-dialog
      :title="title"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose"
    >
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="姓名">
          <el-input v-model="form.realname"></el-input>
        </el-form-item>
        <el-form-item label="用户名">
          <el-input v-model="form.username"></el-input>
        </el-form-item>
        <el-form-item v-if="isPWD" label="密码">
          <el-input type="password" v-model="form.password"></el-input>
        </el-form-item>
        <el-form-item label="角色">
          <el-select
            :multiple="multiple"
            v-model="form.selectValue"
            placeholder="请选择角色"
          >
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="no">取 消</el-button>
        <el-button type="primary" @click="yes">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  props: {
    title: {
      type: String,
      defaultL: "添加用户"
    }
  },
  data() {
    return {
      isPWD: true,
      id: 0,
      multiple: true,
      form: {
        realname: "",
        username: "",
        password: "",
        selectValue: ""
      },
      dialogVisible: false,
      selectValue: null,
      options: []
    };
  },
  created () {
    this.getRole()
  },
  methods: {
    getRole () {
      this.axios.get("http://81.70.46.38:8080/role/list", {
        params: {
          pageNum: 1,
          pageSize: 100
        },
        withCredentials: true
      }).then(res => {
        if (res.data.code === 200) {
          this.list = res.data.result.list;
          var arr = []
          for (var i = 0; i < res.data.result.list.length; i++) {
            arr.push({
              label: res.data.result.list[i].name,
              value: res.data.result.list[i].id
            })
          }
          this.options = arr;
        }
      });
    },
    no() {
      this.dialogVisible = false;
    },
    yes() {
      if (!this.form.realname) {
        this.$message({
          message: "请输入姓名",
          type: "warning"
        });
        return;
      }
      if (!this.form.username) {
        this.$message({
          message: "请输入用户名",
          type: "warning"
        });
        return;
      }
      if (!this.form.realname) {
        this.$message({
          message: "请选择角色",
          type: "warning"
        });
        return;
      }
      if (this.title === "添加用户") {
        if (!this.form.password) {
          this.$message({
            message: "请输入密码",
            type: "warning"
          });
          return;
        }
        this.axios
          .post(
            "http://81.70.46.38:8080/user/addOrUpdate",
            {
              name: this.form.realname,
              username: this.form.username,
              password: this.form.password,
              roleList: this.form.selectValue
            },
            {
              withCredentials: true
            }
          )
          .then(res => {
            if (res.data.code === 200) {
              this.$message({
                message: "添加成功",
                type: "success"
              });
              this.form = {
                realname: "",
                username: "",
                password: "",
                selectValue: ""
              };
              this.$emit('getList')
            }
          });
        this.dialogVisible = false;
      } else {
        this.axios
          .post(
            "http://81.70.46.38:8080/user/addOrUpdate",
            {
              name: this.form.realname,
              username: this.form.username,
              id: this.id,
              roleList: this.form.selectValue
            },
            {
              withCredentials: true
            }
          )
          .then(res => {
            if (res.data.code === 200) {
              this.$message({
                message: "编辑成功",
                type: "success"
              });
              this.form = {
                realname: "",
                username: "",
                password: "",
                selectValue: ""
              };
              this.$emit('getList')
            }
          });
        this.dialogVisible = false;
      }
    }
  }
};
</script>
<style lang="less" scoped></style>
